package com.xizi.treemap;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Set;
import java.util.TreeMap;

@Data
@AllArgsConstructor
@NoArgsConstructor
class Teacher implements Comparable {
    private int age;
    private String name;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = 10;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public int compareTo(Object arg0) {
        if (!(arg0 instanceof Teacher)) {
            return 1;
        }
        Teacher t1 = (Teacher) arg0;
        //按年龄进行排序 升序
        return this.age - t1.getAge();
    }

}

public class MapTest extends Teacher {

    public static void main(String[] args) {
        TreeMap<Integer, String> tm = new TreeMap<>();
        tm.put(0, "zero");
        tm.put(1, "one");
        tm.put(3, "three");
        tm.put(2, "two");
        System.out.println(tm);// {0=zero, 1=one, 2=two, 3=three}
        System.out.println(tm.keySet());// [0, 1, 2, 3]
        System.out.println(tm.values());// [zero, one, two, three]

        Set<Integer> keys = tm.keySet();// set本身就是一个集合
        for (Integer key : keys) {
            System.out.print("学号：" + key + ",姓名：" + tm.get(key) + "\t");
        }

        TreeMap<Teacher, Integer> t = new TreeMap<>();
        Teacher t1 = new Teacher(21, "戏子1");
        Teacher t2 = new Teacher(22, "戏子2");
        t.put(t2, 1);
        t.put(t1, 2);
        System.out.println(t);


    }
}